package edu.hawaii.myisern.model;

import static org.junit.Assert.assertEquals;
import java.util.List;
import org.junit.Test;
import edu.hawaii.myisern.collaborations.jaxb.CollaboratingOrganizations;
import edu.hawaii.myisern.collaborations.jaxb.Collaboration;
import edu.hawaii.myisern.organizations.jaxb.AffiliatedResearchers;
import edu.hawaii.myisern.organizations.jaxb.Organization;
import edu.hawaii.myisern.researchers.jaxb.Researcher;

/**
 * Demonstrates that the MyIsernChecker is working.
 * 
 * @author Edward Kim
 * @author JianFei Liao
 * @author KaYee Leung
 */
public class TestMyIsernChecker {
  /** Singleton instance of MyIsernModel. */
  private MyIsernModel model = MyIsernModel.getInstance();
  
  /**
   * Tests the invalid organizations in the researchers.
   */
  @Test
  public void testInvalidOrganization() {
    // test invalid organization in researcher
    Researcher researcher = new Researcher();
    researcher.setName("New Researcher");
    researcher.setOrganization("Invalid Organization");
    this.model.addOrEditResearcher(researcher);
    
    // make sure the checker catch the invalid organization
    MyIsernChecker checker = new MyIsernChecker(this.model);
    List<String> invalidOrganizations = checker.invalidOrganizationLinksForResearchers();
    for (String string : invalidOrganizations) {
      if (string.contains("Invalid Organization")) {
        System.out.println(string);
        assertEquals("Testing the invalid organizations in researcher",
            "Invalid Organization in researcher: New Researcher", string);
      }
    }
  }
  
  /**
   * Tests the invalid affiliated researchers in organizations.
   */
  @Test
  public void testInvalidResearcher() {
    // test invalid affiliated researchers in organization
    Organization organization = new Organization();
    organization.setName("New Organization");
    AffiliatedResearchers affiliatedResearchers = new AffiliatedResearchers();
    organization.setAffiliatedResearchers(affiliatedResearchers);
    organization.getAffiliatedResearchers().getAffiliatedResearcher().add("Invalid Researcher");
    this.model.addOrEditOrganization(organization);
    
    // make sure the checker catch the invalid affiliated researcher
    MyIsernChecker checker = new MyIsernChecker(this.model);
    List<String> invalidResearchers = checker.invalidResearchersLinksForOrganizations();
    for (String string : invalidResearchers) {
      if (string.contains("Invalid Researcher")) {
        System.out.println(string);
        assertEquals("Testing the invalid affiliated researchers in organization",
            "Invalid Researcher in organization: New Organization", string);
      }
    }
  }
  
  /**
   * Tests the invalid collaborating organizations in collaborations.
   */
  @Test
  public void testInvalidCollabOrganization() {
    // test invalid collaborating organizations in collaboration
    Collaboration collaboration = new Collaboration();
    collaboration.setName("New Collaboration");
    CollaboratingOrganizations collabOrganizations = new CollaboratingOrganizations();
    collaboration.setCollaboratingOrganizations(collabOrganizations);
    collaboration.getCollaboratingOrganizations().getCollaboratingOrganization()
        .add("Invalid Collaborating Organization");
    this.model.addOrEditCollaboration(collaboration);
    
    // make sure the checker catch the invalid collaborating organization
    MyIsernChecker checker = new MyIsernChecker(this.model);
    List<String> invalidCollabOrganizations = 
        checker.invalidCollabOrganizationsLinksForCollaborations();
    for (String string : invalidCollabOrganizations) {
      if (string.contains("Invalid Collaborating Organization")) {
        System.out.println(string);
        assertEquals("Testing the collaborating organizations in collaborations",
            "Invalid Collaborating Organization in collaboration: New Collaboration", string);
      }
    }
  }
}